﻿// controller to handle the user's library
angular.module('app.user').controller('UserLibraryController', [
	'$scope', 'dataService', 'libraryMediator', 'notify',
	function ($scope, dataService, libraryMediator, notify) {

		var instance = this;

		$scope.model.currentFolder = null;
		$scope.model.tempImages = null;
		$scope.model.newFolderName = null;
		$scope.model.isMovingImage = false;
		$scope.model.imageDestinationFolder = null;
		// notify that an image is being moved
		$scope.notifyBeginMoveImage = function () {
			$scope.model.isMovingImage = true;
		};
		// notify an image is no longer being moved
		$scope.notifyEndMoveImage = function () {
			$scope.model.isMovingImage = false;
		};
		// set the destination folder for the image being moded
		$scope.setImageDestinationFolder = function (folder) {
		    $scope.model.imageDestinationFolder = folder;
		    $scope.model.newFolderName = folder.Name;
		};
		// set the current folder to the passed folder
		$scope.setCurrentFolder = function (folder) {
			// set the current folder
			$scope.model.currentFolder = folder;
			// hold on to the temporary images
			var images = $scope.model.tempImages;
			// just in case the images are loaded before the folder is changed, set the images back
			if ($scope.model.currentFolder !== null &&
				$scope.model.currentFolder !== undefined &&
				images !== null &&
				images !== undefined &&
				images.length > 0 &&
				$scope.model.currentFolder.Id === images[0].FolderId) {
				$scope.model.currentFolder.Images = images;
			}
		};
		// set the images for the current folder
		$scope.setImagesForCurrentFolder = function (images) {
			// set the temporary images
			$scope.model.tempImages = images;
			// if the current folder matches the images, then set the folder images
			if ($scope.model.currentFolder !== null &&
				$scope.model.currentFolder !== undefined &&
				images !== null &&
				images !== undefined &&
				images.length > 0 &&
				$scope.model.currentFolder.Id === images[0].FolderId) {
				$scope.model.currentFolder.Images = images;
			}
		};
		// handle the cascade of folder privacy
		this.setSubFoldersAndImagesPrivacy = function (folder, isPrivate) {
			// The 'actual' data change will happen on the server
			// handle sub folders and images
			if (folder.Folders !== null && folder.Folders !== undefined && folder.Folders.length > 0) {
				// for each sub folder, set the parent folder privacy
				angular.forEach(folder.Folders, function (subFolder) {
					subFolder.IsParentFolderPrivate = isPrivate;
					// do not cascade if the sub folder is explictly private
					if (!subFolder.IsPrivate) {
						instance.setSubFoldersAndImagesPrivacy(subFolder, isPrivate);
					}
				});
			}
			// set the privacy for the images within the folder
			if (folder.Images !== null && folder.Images !== undefined && folder.Images.length > 0) {
				angular.forEach(folder.Images, function (image) {
					image.IsFolderPrivate = isPrivate;
				});
			}
		};
		// set the folder as private
		$scope.lockFolder = function (folder) {
			// upload the data change to the server
			dataService.upload('Folder', { Id: folder.Id, IsPrivate: true }).then(function (response) {
				// check for successful response
				var data = response.data;
				if (data === null || data === undefined ||
					data.Content === null || data.Content === undefined) {
					return;
				}

				// upload was successful, set to private and cascade the change to subfolders and images
				if (data.Successful) {
					folder.IsPrivate = true;
					instance.setSubFoldersAndImagesPrivacy(folder, true);
				}
			});
		};
		// set the folder as public
		$scope.unlockFolder = function (folder) {
			// upload the data change to the server
			dataService.upload('Folder', { Id: folder.Id, IsPrivate: false }).then(function (response) {
				// check for successful response
				var data = response.data;
				if (data === null || data === undefined ||
					data.Content === null || data.Content === undefined) {
					return;
				}
				// upload was successful, set to public and cascade the change to subfolders and images
				if (data.Successful) {
					folder.IsPrivate = false;
					instance.setSubFoldersAndImagesPrivacy(folder, false);
				}
			});
		};
		// submit the new name for the folder to the server
		$scope.submitRenameFolder = function () {
			libraryMediator.renameFolder($scope.model.currentFolder);
		};
	}]);